﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;
using WeixinBusiness;
using WeixinBusiness.Core;
using WeixinBusiness.Model;

namespace MenuWeb
{
    public partial class WechatMenu : System.Web.UI.Page
    {

        public static string xmlPath = System.AppDomain.CurrentDomain.BaseDirectory;
        public static string xmlUrl = Path.Combine(xmlPath, "MenuSetting.xml");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!File.Exists(xmlUrl))
            {
                List<MenuSetting> list = new List<MenuSetting>();
                Common.SaveXml<MenuSetting>(xmlUrl, list);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            var pwd = Request.Params["pwd"];
            var validPwd = string.Format("{0}qsk", DateTime.Now.Hour);
            if (string.IsNullOrWhiteSpace(pwd) || !string.Equals(pwd, validPwd))
            {
                Response.Write("授权失败");
                Response.End();
            }
            base.OnInit(e);
        }

        [WebMethod]
        public static string GetDataList()
        {
            var list = MenuCore.GetAll(xmlUrl);
            JsonResult model = new JsonResult();
            if (list.Count() > 0)
            {
                model.Status = true;
                model.Datas.AddRange(list);
            }
            else
            {
                model.Status = false;
                model.Msg = "查不到相关配置信息";
            }
            JavaScriptSerializer json = new JavaScriptSerializer();
            return json.Serialize(model);
        }
        [WebMethod]
        public static string InsertData(string menuName, string parentId, int menuType, string menuKey, string menuUrl, int orderBy)
        {
            JavaScriptSerializer json = new JavaScriptSerializer();
            JsonResult model = new JsonResult();
            try
            {
                if (string.IsNullOrWhiteSpace(menuName))
                {
                    model.Status = false;
                    model.Msg = "请输入菜单名称";
                    return json.Serialize(model);
                }
                var list = MenuCore.GetAll(xmlUrl);
                if (!string.IsNullOrWhiteSpace(parentId))
                {
                    if (!list.Any(x => x.MenuId == parentId))
                    {
                        model.Status = false;
                        model.Msg = "找不到对应的父级菜单";
                        return json.Serialize(model);
                    }
                }
                MenuSetting menu = new MenuSetting();
                menu.MenuName = menuName;
                menu.ParentId = parentId;
                menu.MenuType = menuType;
                menu.MenuKey = menuKey;
                menu.MenuUrl = menuUrl;
                menu.Orderby = orderBy;
                menu.CreateTime = DateTime.Now;
                menu.ModifyTime = DateTime.Now;
                MenuCore.InsertMenuSetting(menu, xmlUrl);
                list = MenuCore.GetAll(xmlUrl);
                if (list.Count() > 0)
                {
                    model.Status = true;
                    model.Msg = "修改成功";
                    model.Datas.AddRange(list);
                }
            }
            catch (Exception ex)
            {
                model.Status = false;
                model.Msg = ex.Message;
            }
            return json.Serialize(model);
        }
        [WebMethod]
        public static string UpdateData(string menuId, string menuName, string parentId, int menuType, string menuKey, string menuUrl, int orderBy)
        {
            JavaScriptSerializer json = new JavaScriptSerializer();
            JsonResult model = new JsonResult();
            try
            {
                if (string.IsNullOrWhiteSpace(menuId) || string.IsNullOrWhiteSpace(menuName))
                {
                    model.Status = false;
                    model.Msg = "有必要元素为空";
                    return json.Serialize(model);
                }
                var updateItem = MenuCore.Get(menuId, xmlUrl);
                if (updateItem == null)
                {
                    model.Status = false;
                    model.Msg = "找不到对应数据";
                    return json.Serialize(model);
                }
                updateItem.MenuName = menuName;
                updateItem.MenuType = menuType;
                updateItem.MenuKey = menuKey;
                updateItem.MenuUrl = menuUrl;
                updateItem.Orderby = orderBy;
                updateItem.ParentId = parentId;
                MenuCore.UpdateMenuSetting(updateItem, xmlUrl);
                var list = MenuCore.GetAll(xmlUrl);
                if (list.Count() > 0)
                {
                    model.Status = true;
                    model.Msg = "修改成功";
                    model.Datas.AddRange(list);
                }
            }
            catch (Exception ex)
            {
                model.Status = false;
                model.Msg = ex.Message;
            }
            return json.Serialize(model);
        }

        [WebMethod]
        public static string DeleteData(string menuId)
        {
            JavaScriptSerializer json = new JavaScriptSerializer();
            JsonResult model = new JsonResult();
            try
            {
                if (string.IsNullOrWhiteSpace(menuId))
                {
                    model.Status = false;
                    model.Msg = "无效的提交数据";
                    return json.Serialize(model);
                }
                var deleteItem = MenuCore.Get(menuId, xmlUrl);
                if (deleteItem == null)
                {
                    model.Status = false;
                    model.Msg = "无效的提交数据";
                    return json.Serialize(model);
                }
                MenuCore.DeleteMenuSetting(menuId, xmlUrl);
                var list = MenuCore.GetAll(xmlUrl);
                if (list.Count() > 0)
                {
                    model.Status = true;
                    model.Msg = "删除成功";
                    model.Datas.AddRange(list);
                }
            }
            catch (Exception ex)
            {
                model.Status = false;
                model.Msg = ex.Message;
                return json.Serialize(model);
            }
            return json.Serialize(model);
        }
        [WebMethod]
        public static string PublishMenu(string accessToken)
        {
            if (string.IsNullOrWhiteSpace(accessToken))
            {
                return "请输入accessToken";
            }
            return MenuCore.PublishWechatMenu(xmlUrl, accessToken);
        }

    }

    public class JsonResult
    {
        public JsonResult()
        {
            Datas = new List<object>();
        }
        public bool Status { get; set; }

        public string Msg { get; set; }

        public List<object> Datas { get; set; }
    }
}